from setting.default import MYaaa
from werkzeug.routing import BaseConverter
from flask import Flask
from flask import request
from flask import render_template
from flask import redirect
from flask import url_for
from flask import jsonify
from flask import make_response
import json

def create_flask_app(aaa):
    app = Flask(__name__)
    app.config.from_object(aaa)
    return app

app = create_flask_app(MYaaa)



# 路由传参固定参数
# class PhoneConverter(BaseConverter): # 自定义转化器类
#     # 自定义正则表达式匹配模式
#     regex = r'1[3-9]\d{9}'
# # 添加至converters容器
# app.url_map.converters['phone'] = PhoneConverter

# @app.route('/stus/<phone:id>/')
# def index(id):
#     print('我的手机号是>',id)
#     return '成功'

# 查询字符串
# http://127.0.0.1:5000/stus?name=123&age=18
# @app.route('/stus')
# def stu():
#     name = request.args.get('name')
#     age = request.args.get('age')
#     print(name,age)
#     return '获取学生信息成功'

# 获取表单数据
# @app.route('/stus',methods=['post'])
# def Stu():
#     name = request.form.get('username')
#     print(name)
#     return '获取表单数据'

# 获取文件上传
# @app.route('/files',methods=['post'])
# # def postfiles():
# #     file = request.files.get('name')
# #     print('当前文件名>',file.filename)
# #     return '文件获取成功'

# 其他参数
# @app.route('/index')
# def index():
#     print(request)
#     print('当前的请求方式',request.method)
#     print('当前的请求路由',request.url)
#     print('当前的请求数据',request.data) # 返回的数据类型是字节类型
#     print('当前请求的值',request.values)
#     print('cookie',request.cookies)
#     return '其他参数'

# 模板的响应
# @app.route('/index',endpoint='aaa')
# def index():
#     # render_template 默认会从templates目录下查找html页面
#     return render_template('index.html')
#
# # 重定向的响应
# @app.route('/login',methods=['post','get'])
# def login():
#     name = request.form.get('username')
#     pwd = request.form.get('pwd')
#     print('当前的用户名是》》',name)
#     # return redirect('/index')
#     return redirect(url_for('aaa'))  # url_for 找到传入endpoint对应的路由

# 响应json数据
# @app.route('/users/<id>/')
# def user_info(id):
#     data_ = {
#         'id':id,
#         'name':'hahaha',
#         'address':'东碑',
#     }
#     # return json.dumps(data_)
#     # return jsonify(data_)
#     # return jsonify(id=id,name='hahaha')
#
#     # 响应元祖  （response,status,headers）
#     # response 响应的数据
#     # status 状态码 会覆盖自带的
#     # 头部 可以不传 字典形式
#     return ('黄河九曲十八弯','400',{'auth':'china'})

# 自定义响应
@app.route('/users/')
def user_info():
    file = request.files.get('name')
    img_name = file.filename
    # 创建响应对象
    resp = make_response(img_name)
    # 制定相应对象类型
    resp.headers['Content-Type'] = 'image/png'
    resp.status = '204'
    return resp

if __name__ == '__main__':
    app.run()